﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace rssilocalizer
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }


        private class Output
        {
            public List<int> router_rssis;
            public double x;
            public double y;
        }

        private void button1_Click(object sender, EventArgs e)
        {  Add_RSSI();
           Print();
        }

        List<Output> outputs = new List<Output>();
        double x;
        double y;

        private void Add_RSSI()
        {
                string file = File.ReadAllText("combined_routers_C.txt");
                string[] lines = file.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
                
                Output output = new Output();
                output.router_rssis = new List<int>(8);

                for (int i = 0; i < 8; i++)
                {
                    output.router_rssis.Add(int.MaxValue);
                }

                foreach (string line in lines)
                {
                    string line2 = line.Trim();
                    string[] fields = line2.Split(new char[] { '\t' });
                    try
                    {
                        if (fields[1] != string.Empty)
                        {
                            int nnVector = int.Parse(fields[1]) - 1;
                            int rssi_val = int.Parse(fields[2]);
                            output.router_rssis[nnVector] = rssi_val;
                        }
                        else
                        {
                            x = double.Parse(fields[3]);
                            y = double.Parse(fields[4]);
                        }

                        bool full = true;
                        foreach (int rssi in output.router_rssis)
                        {
                            if (rssi.Equals(int.MaxValue))
                            {
                                full = false;
                                break;
                            }
                        }
                        if (full)
                        {
                            output.x = x;
                            output.y = y;

                            outputs.Add(output);
                            output = new Output();
                            output.router_rssis = new List<int>(8);
                            for (int i = 0; i < 8; i++)
                            {
                                output.router_rssis.Add(int.MaxValue);
                            }
                        }
                    }
                    catch 
                    {                  
                    }
                }                
        }


        private void Print()
        {
            File.Delete("targets.txt");
            File.Delete("inputs.txt");
            bool first;
            int max_reciver_count = 8;
            first = true;
            bool second = false;
            string targets = string.Empty;
            string inputs = string.Empty;

            for (int x = 0; x < max_reciver_count; x++)
            {
                foreach (Output o1 in outputs)
                {
                    if (first)
                    {
                        targets += o1.x.ToString() + "\t";
                        txtTargets.AppendText(o1.x.ToString() + "\t");
                    }
                    else if (second)
                    {
                        targets += o1.y.ToString() + "\t";
                        txtTargets.AppendText(o1.y.ToString() + "\t");
                    }

                    inputs += o1.router_rssis[x].ToString() + "\t";
                    txtInputs.AppendText(o1.router_rssis[x].ToString() + "\t");
                }
                inputs.TrimEnd(new char[] { '\t' });
                inputs += "\r\n";

                txtInputs.Text.TrimEnd(new char[] { '\t' });
                txtInputs.AppendText("\r\n");
                if (first)
                {
                    targets.TrimEnd(new char[] { '\t' });
                    targets += "\r\n";

                    txtTargets.Text.TrimEnd(new char[] { '\t' });
                    txtTargets.AppendText("\r\n");
                    first = false;
                    second = true;
                }
                else if (second)
                    second = false;

            }
            File.WriteAllText("inputs.txt", inputs);
            File.WriteAllText("targets.txt", targets);

            MessageBox.Show("Done!");
        }


    }
}
